<template>
	<view class="book-page">
		<view class="book-header">
			<view>
				<view class="line-time line-subTitle">
					房间类型：{{dealProductType}}
				</view>
				<view class="line-time line-subTitle">
					原包间：{{orderDetail.productName}}
				</view>
				<view class="line-time" style="color: #666;">
					原订单时长：{{orderDetail.hour}}小时
				</view>
				<view class="line-time" style="color: #666;">
					剩余时长：{{orderDetail.minute}}分钟
				</view>
				<view class="line-time" style="color: #666;">
					原订单时间段：{{orderDetail.actualStartTime}} ~ {{orderDetail.actualEndTime}}
				</view>
				<view class="line-time flex-sb line-title">
					<view>选择更换的包间：</view>
					<picker @change="roomPickerChange" :range="roomArray" range-key="productName">
						<view class="picker-tip">{{currentRoom.productName || '请选择更换的包间'}}<uni-icons color="#000"
								type="right" size="16"></uni-icons></view>
					</picker>
				</view>
			</view>
			<view class="date-box" v-if="false">
				<view class="date-item" @click="changeDate(index)" :class="currentDate === index?'act-btn':''"
					v-for="(item,index) in dateRangArr" :key="index">
					<view class="date-tltle">{{item.week}}</view>
					<view class="date-text">{{item.monthDay}}</view>
				</view>
			</view>
		</view>
		<view class="book-main" v-if="false" v-show="currentRoom.productId">
			<!-- <view class="time-main">
				<view class="package-title">时间选择</view>
				<view class="time-box">
					<uni-number-box :min="1" :max="23" :disabled="true" @change="changeTime" :value="hour" />
				</view>
			</view> -->
			<view class="package-title">时间选择</view>
			<view class="time-box time-middle-box">
				<view class="time-date" v-if="packageObj.startTime">{{packageObj.startTime.substring(0,10)}}</view>
				<picker class="custom-picker" mode="multiSelector" :value="multiIndex" :range="newTimeList"
					range-key="label" @change="pickerChange" @columnchange="pickerColumnchange">
					<view class="time-text">{{startTime.substring(11) || '请选择开始时间'}}</view>
				</picker>
				<text class="time-mid">至</text>
				<view class="time-date" v-if="packageObj.endTime">{{packageObj.endTime.substring(0,10)}}</view>
				<text>{{endTime || '结束时间'}}</text>
			</view>
			<view class="time-line-box">
				<timeLineBar :bookingTimeVo="bookingTimeVo"></timeLineBar>
			</view>
		</view>

		<view class="footer-btn">
			<view class="btn" @click="saveSubmit">确认更换</view>
		</view>
	</view>
</template>

<script>
	import timeLineBar from '@/components/time-line-bar.vue'
	import dayjs from 'dayjs'
	export default {
		components: {
			timeLineBar
		},
		computed:{
			dealProductType(){
				let type = ''
				this.productionModel.forEach((item,index)=>{
					if(item.modelType === this.orderDetail.productType){
						type = item.modelName
					}
				})
				return type
			}
		},
		data() {
			return {
				productionModel:[],
				packageObj: {},
				orderDetail: {},
				bookingTimeVo: {},
				currentDate: 0,
				dateRangArr: [],
				currentRoom: {
					productName: '',
					productId: ''
				},
				roomArray: [],
				orderId: '',
				hour: 1,
				minute: 0,
				second: '00',
				newTimeList: [
					[],
					[]
				],
				multiIndex: [0, 0],
				startTime: '',
				endTime: '',
				shopId: '',
				isOrder: false
			}
		},
		methods: {
			// 选择门店
			roomPickerChange(e) {
				this.currentRoom = this.roomArray[e.detail.value]
				this.validateTimeEvent()
			},
			// 选择日期
			changeDate(index) {
				this.currentDate = index
				this.validateTimeEvent()
				this.getProductTime()
			},
			// 选择时长
			changeTime(e) {
				this.$nextTick(() => {
					this.hour = e
					if (this.startTime) {
						this.endTime = dayjs(this.startTime).add(this.minute, 'minute').format(
							'YYYY-MM-DD HH:mm:ss')
					}
				})
			},
			// 获取日期的预定情况
			getProductTime() {
				this.$request({
					method: 'get',
					url: `/applet/store/product/bookingTime/${this.orderDetail.productId}/${this.dateRangArr[this.currentDate].date}`,
					loading: true,
					isEncrypt: false
				}).then(res => {
					this.bookingTimeVo = res.data
					this.initTime()
				})
			},
			initTime() {
				this.newTimeList[0] = this.bookingTimeVo.timeList.map((item, index) => {
					// console.log('item:',item);
					var obj = {
						label: String(item.hour),
						value: item.hour
					}
					return obj
				})
				this.newTimeList[1] = this.bookingTimeVo.timeList[this.multiIndex[0]].minuteList.map((item, index) => {
					// console.log('item:',item);
					var obj = {
						label: String(item),
						value: item
					}
					return obj
				})
			},
			pickerChange(e) {
				//	console.log('pickerChange:', e.detail.value);
				let numArr = e.detail.value
				let hour = this.bookingTimeVo.timeList[numArr[0]].hour
				let minute = this.bookingTimeVo.timeList[numArr[0]].minuteList[numArr[1]]
				if (hour < 10) {
					hour = `0${hour}`
				}
				if (minute < 10) {
					minute = `0${minute}`
				}
				let time = `${hour}:${minute}`
				this.startTime = `${this.dateRangArr[this.currentDate].date} ${time}:${this.second}`
				this.endTime = dayjs(this.startTime).add(this.minute, 'minute').format('YYYY-MM-DD HH:mm:ss')
			},
			pickerColumnchange(e) {
				// 第几列滑动
				// console.log(e.detail.column);
				// 第几列滑动的下标
				// console.log(e.detail.value)
				// 第一列滑动
				if (e.detail.column === 0) {
					this.multiIndex[0] = e.detail.value;
					this.newTimeList[1] = this.bookingTimeVo.timeList[this.multiIndex[0]].minuteList.map((item, index) => {
						// console.log('item:',item);
						var obj = {
							label: String(item),
							value: item
						}
						return obj
					})
					this.multiIndex.splice(1, 1)
				}
				if (e.detail.column === 1) {
					this.multiIndex[1] = e.detail.value
				}
			},
			// 获取产品预定详细信息
			getProductionDetail() {
				this.$request({
					method: 'get',
					url: `/applet/store/product/bookingInfo/${this.orderDetail.productId}`,
					loading: true,
					isEncrypt: false
				}).then(res => {
					const {
						bookDateList
					} = res.data
					this.dateRangArr = bookDateList
					this.getProductTime()
					this.validateTimeEvent()
				})
			},
			// 获取产品类型
			getProductionModel(){
				this.$request({
					method: 'get',
					url: `/applet/store/product/modelList`,
					loading: true,
					isEncrypt: false
				}).then(res => {
					this.productionModel = res.data || []
				}).catch(err => {
					console.log('查询异常：', err)
				})
			},
			// 获取延时详情
			getDelayDetail() {
				this.$request({
					method: 'post',
					url: `/applet/store/order/delayInfo/${this.orderId}`,
					loading: true,
					isEncrypt: false
				}).then(res => {
					const {
						bookingTimeVo,
						orderDetail
					} = res.data
					this.bookingTimeVo = bookingTimeVo
					this.orderDetail = orderDetail
					this.hour = orderDetail.hour
					this.minute = orderDetail.minute
					this.startTime = orderDetail.actualStartTime 
					this.endTime = orderDetail.actualEndTime
					this.second = this.orderDetail.actualStartTime.substring(17)
					this.getProductionDetail()
					this.getRoomList()
				}).catch(err => {
					console.log('请求异常：', err)
				})
			},
			//校验产品时间是否占用
			validateTimeEvent() {
				let startTime =
					`${this.dateRangArr[this.currentDate].date} ${this.orderDetail.actualStartTime.substring(11)}`
				let endTime = dayjs(startTime).add(this.minute, 'minute').format('YYYY-MM-DD HH:mm:ss')
				const data = {
					productId: this.currentRoom.productId,
					startTime,
					endTime
				}
				this.$request({
					method: 'post',
					url: `/applet/store/product/validateTime`,
					data,
					loading: true,
					isEncrypt: false
				}).then(res => {
					if (res.data === false) {
						this.countTime()
					} else {
						this.startTime = ''
						this.endTime = ''
					}
				}).catch(err => {
					console.log('请求异常：', err)
				})
			},
			// 计算时间
			countTime() {
				this.startTime =
					`${this.dateRangArr[this.currentDate].date} ${this.orderDetail.actualStartTime.substring(11)}`
				this.endTime = dayjs(this.startTime).add(this.minute, 'minute').format('YYYY-MM-DD HH:mm:ss')
			},
			// 请求包间列表
			getRoomList() {
				const data = {
					shopId: this.shopId,
					modelType: Number(this.orderDetail.productType)
				}
				this.$request({
					method: 'post',
					url: `/applet/store/product/list`,
					data,
					loading: true,
					isEncrypt: false
				}).then(res => {
					const {
						rows
					} = res
					this.roomArray = res.rows || []
				})
			},
			// 更换包间
			saveSubmit() {
				if (!this.currentRoom.productId) {
					uni.showToast({
						title: '请选择更换的包间',
						duration: 2000,
						icon: 'none'
					})
					return
				} else if (!this.startTime) {
					uni.showToast({
						title: '请选择开始时间',
						duration: 2000,
						icon: 'none'
					})
					return
				}
				const data = {
					orderId: this.orderId,
					productId: this.currentRoom.productId,
					startTime: this.startTime,
					endTime: this.endTime
				}
				this.$request({
					method: 'post',
					url: `/applet/store/order/changeProduct`,
					data,
					loading: true,
					isEncrypt: false
				}).then(res => {
					if (res.code === 200) {
						uni.showToast({
							title: '更换包间成功',
							duration: 2000,
							icon: 'none'
						})
						setTimeout(() => {
							if (this.isOrder) {
								const eventChannel = this.getOpenerEventChannel()
								eventChannel.emit('reload')
							}
							uni.navigateBack()
						}, 2000)
					} else {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'none'
						})
					}
				}).catch(err => {
					console.log('请求异常：', err)
				})
			}
		},
		onLoad(option) {
			this.orderId = option.orderId
			this.shopId = option.shopId
			if (option?.isOrder === 'true') {
				this.isOrder = true
			}
			this.getDelayDetail()
			this.getProductionModel()
		}
	}
</script>

<style lang="scss" scoped>
	.book-page {
		height: 100vh;

		.package-title {
			font-size: 32rpx;
			color: #333333;
			margin-bottom: 20rpx;
			font-weight: bold;
		}

		.time-main {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 30rpx;
			font-size: 32rpx;
		}

		.time-middle-box {
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.time-box {
			display: flex;
			align-items: center;
			color: #000;
			font-weight: bold;
		}

		.time-text {
			border-radius: 8rpx;
			border: 1px solid #000;
			padding: 10rpx;
			font-size: 32rpx;
			text-align: center;
		}

		.time-mid {
			margin: 0 20rpx;
		}

		.time-date {
			margin-right: 10rpx;
			color: #000;
		}

		.time-line-box {
			margin-top: 30rpx;
		}

		.footer-btn {
			width: 100%;
			position: absolute;
			bottom: 0;
			padding: 30rpx;
			box-sizing: border-box;
			display: flex;

			.btn {
				flex: 1;
				text-align: center;
				border-radius: 40rpx;
				padding: 20rpx;
				background: #ff9600;
				color: #FFF;
			}
		}

		.flex-sb {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.book-header {
			background: #ffd589;
			padding: 30rpx;
		}

		.book-title {
			font-weight: bold;
			font-size: 32rpx;
			color: #333333;
			margin-bottom: 20rpx;
		}

		.main-btns {
			display: flex;
			flex-wrap: wrap;

			.tag {
				background: #FFF;
				border-radius: 4rpx;
				border: 1rpx solid #1D7F69;
				color: #1D7F69;
				padding: 2rpx 4rpx;
				margin-right: 10rpx;
				font-size: 20rpx;
				margin-top: 10rpx;
			}
		}

		.line-time {
			margin-top: 10rpx;
			font-size: 24rpx;
			color: #000;
			display: flex;
			align-items: center;
		}

		.line-title {
			font-size: 32rpx;
		}

		.line-subTitle {
			font-size: 28rpx;
		}

		.icon-box {
			display: flex;
			align-items: center;
		}

		.icon-sty-01 {
			width: 22rpx;
			height: 22rpx;
			margin-right: 10rpx;
		}

		.icon-sty-02 {
			width: 20rpx;
			height: 26rpx;
			margin-right: 10rpx;
		}

		.date-box {
			display: flex;
			flex-wrap: wrap;
			padding: 10rpx 0;
			margin-top: 20rpx;

			.date-item {
				flex: 1;
				margin: 0 10rpx;
				background: #FFF;
				text-align: center;
				padding: 10rpx;
				border-radius: 10rpx;
				box-sizing: border-box;

				.date-tltle {
					font-weight: bold;
					font-size: 24rpx;
					color: #333333;
				}

				.date-text {
					font-size: 18rpx;
					color: #333333;
				}
			}

			.act-btn {
				border: 1px solid #f54d54;
				background: #fff9e3;
				position: relative;
			}

			.act-btn::after {
				content: '';
				position: absolute;
				width: 10rpx;
				height: 10rpx;
				background: #fff9e3;
				border-bottom: 1px solid #f54d54;
				border-right: 1px solid #f54d54;
				bottom: -8rpx;
				left: calc(50% - 5rpx);
				transform: rotate(45deg);
			}
		}

		.book-main {
			width: 100%;
			background: #FFF;
			padding: 30rpx;
			border-radius: 40rpx 40rpx 0 0;
			overflow: hidden;
			margin-top: -26rpx;
			box-sizing: border-box;
		}

		.tabs {
			display: flex;
			align-items: center;
			width: 100%;
			height: 100rpx;
			box-sizing: border-box;
			background-color: #fff;
			box-shadow: 0px 4px 10rpx rgba(0, 0, 0, 0.1);
		}



	}
</style>